<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增标签')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-label-add">
            <input id="treeId" name="parentId" type="hidden" th:value="${label.labelId}"   />
            <input name="parentName" type="hidden" th:value="${label.labelName}">
            <div class="form-group">
                <label class="col-sm-3 control-label">上级标签：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="text" onclick="selectLabelTree()" id="treeName" readonly="true" th:value="${label.labelName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标签名称：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="labelName" id="labelName" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">显示排序：</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" name="orderNum" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
        var prefix = ctx + "control/label"

        $("#form-label-add").validate({
            onkeyup: false,
            rules:{
                labelName:{
                    remote: {
                        url: prefix + "/checkLabelNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "parentId": function() {
                                return $("input[name='parentId']").val();
                            },
                            "labelName" : function() {
                                return $.common.trim($("#labelName").val());
                            },
                            "token" : storage.get("token")
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
                orderNum:{
                    digits:true
                },
            },
            messages: {
                "labelName": {
                    remote: "标签已经存在"
                }
            },
            focusCleanup: true
        });
        /*标签-新增-选择父源点树*/
        function selectLabelTree() {
            var treeId = $("#treeId").val();
            var menuId = treeId > 0 ? treeId : 1;
            var url = prefix + "/selectLabelTree/" + menuId;
            var options = {
                title: '标签选择',
                width: "380",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }
        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-label-add').serialize());
            }
        }
        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            $('input[name="parentName"]').val(body.find('#treeName').val());
            layer.close(index);
        }
    </script>
</body>
</html>
